var express = require('express');
var router = express.Router();
//引入连接池模块
const pool = require('../pool');
//1.首页轮播图数据获取
//接口地址：http://127.0.0.1:3000/carousel
// 请求方式:get
router.get('/carousel', function (req, res, next) {
  pool.query("select * from hchw_carousel", (err, result) => {
    if (err) {
      next(err);
      return;
    }
    res.json(result)
  })
});
//2.商品栏目信息获取(按类别来)
//接口地址：http://127.0.0.1:3000/item/1
// 请求方式:get
router.get('/item/:family_id', function (req, res, next) {
  let obj=req.params;
  pool.query("select * from hchw_laptop where family_id =?", [obj.family_id],(err, result) => {
    if (err) {
      next(err);
      return;
    }
    res.json(result)
  })
});
//3.获取商品详情图下部接口
//接口地址：http://127.0.0.1:3000/detail/:1
// 请求方式:get
router.get("/details/:laptop_id", (req, res, next) => {
  let obj = req.params;
  console.log(obj);
  pool.query("select * from hchw_laptop_pic where laptop_id=?", [obj.laptop_id], (err, result) => {
    if (err) {
      next(err);
      return;
    }
    res.json({ results: result });
  })
})
//4.获取商品分类名称
// 请求方式：get
// 路由地址：http://127.0.0.1:3000/fenlei
router.get('/fenlei', (req, res) => {
  // let fid=req.query.fid;
  var sql = "select * from hchw_laptop_family";
  pool.query(sql, (err, result) => {
    if (err) throw err;
    res.json(result)
  })
});
//5、 获取商品分类的商品
// 请求方式：get
// 路由地址：http://127.0.0.1:3000/classify?
// router.get("/classify/1", (req, res) => {
//   let fid = req.query.fid;
//   console.log(req.query);
//   let sql = "select * from list inner join hchw_laptop_family on hchw_laptop_family_fid=fid where fid=?";
//   pool.query(sql, [fid], (err, result) => {
//     if (err) throw err;
//     res.send(result)
//   })
// })
//6.商品栏目信息获取(按类别来不是1类)
//接口地址：http://127.0.0.1:3000/items
// 请求方式:get
router.get('/items', function (req, res, next) {
  pool.query("select * from hchw_laptop where family_id !=1",(err, result) => {
    if (err) {
      next(err);
      return;
    }
    res.json(result)
  })
});
//7.搜索商品是否存在详情图上部接口
//接口地址：http://127.0.0.1:3000/search/:title
// 请求方式:get
router.get("/search/:title", (req, res, next) => {
  let obj = req.params;
  console.log(obj);
  // res.send("请求成功")
  pool.query(`select * from hchw_laptop where title like '%${obj.title}%'`, (err, result) => {
    if (err) {
      next(err);
      return;
    }
    res.json({ results: result });
  })
})
//8.获取商品详情图上部接口
//接口地址：http://127.0.0.1:3000/detail/:lid
// 请求方式:get
router.get("/detail/:lid", (req, res, next) => {
  let obj = req.params;
  console.log(obj);
  pool.query("select * from hchw_laptop where lid=?", [obj.lid], (err, result) => {
    if (err) {
      next(err);
      return;
    }
    res.json({ results: result });
  })
})
module.exports = router;
